//
// Source code recreated from ConfigVo .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ht.modules.system.online.cgform.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import jakarta.annotation.Resource;
import org.apache.shiro.SecurityUtils;
import com.ht.entity.LoginUser;
import com.ht.utils.oConvertUtils;
import com.ht.modules.system.online.cgform.entity.OnlCgformField;
import com.ht.modules.system.online.cgform.mapper.OnlCgformFieldMapper;
import com.ht.modules.system.online.cgform.service.IOnlCgformFieldService;
import com.ht.modules.system.online.cgform.util.CgformUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class OnlCgformFieldServiceImpl extends ServiceImpl<OnlCgformFieldMapper, OnlCgformField> implements IOnlCgformFieldService {
    private static final Logger log = LoggerFactory.getLogger(OnlCgformFieldServiceImpl.class);
    @Resource
    private OnlCgformFieldMapper onlCgformFieldMapper;

    public OnlCgformFieldServiceImpl() {
    }

    public Map<String, Object> queryAutolistPage(String tbname, String headId, Map<String, Object> params) {
        Map<String, Object> result = new HashMap();
        LambdaQueryWrapper<OnlCgformField> query = new LambdaQueryWrapper();
        query.eq(OnlCgformField::getCgformHeadId, headId);
        List<OnlCgformField> allFieldList = this.list(query);
        List<OnlCgformField> fieldList = this.queryAvailableFields(tbname, true, allFieldList);
        StringBuffer sb = new StringBuffer();
        CgformUtil.getAutoListBaseSql(tbname, fieldList, sb);
        String conditionSql = CgformUtil.getAutoListConditionSql(allFieldList, params);
        sb.append(" where 1=1  " + conditionSql);
        String order = params.get("order").toString();
        String column = params.get("column").toString();
        if (this.hasField(column, allFieldList)) {
            sb.append(" ORDER BY " + oConvertUtils.camelToUnderline(column));
            if ("asc".equals(order)) {
                sb.append(" asc");
            } else {
                sb.append(" desc");
            }
        }

        Integer pageSize = params.get("pageSize") == null ? 10 : Integer.parseInt(params.get("pageSize").toString());
        Integer pageNo = params.get("pageNo") == null ? 1 : Integer.parseInt(params.get("pageNo").toString());
        Page<Map<String, Object>> page = new Page((long)pageNo, (long)pageSize);
        log.info("---Online查询sql:>>" + sb.toString());
        IPage<Map<String, Object>> pageList = this.onlCgformFieldMapper.selectPageBySql(page, sb.toString());
        result.put("total", pageList.getTotal());
        result.put("records", CgformUtil.toLowerCasePageList(pageList.getRecords()));
        return result;
    }

    public void saveFormData(String code, String tbname, JSONObject json) {
        LambdaQueryWrapper<OnlCgformField> query = new LambdaQueryWrapper();
        query.eq(OnlCgformField::getCgformHeadId, code);
        List<OnlCgformField> fieldList = this.list(query);
        Map<String, Object> params = CgformUtil.getFormDataSaveSql(tbname, fieldList, json);
        ((OnlCgformFieldMapper)this.baseMapper).executeInsertSQL(params);
    }

    public void saveFormData(List<OnlCgformField> fieldList, String tbname, JSONObject json) {
        Map<String, Object> params = CgformUtil.getFormDataSaveSql(tbname, fieldList, json);
        ((OnlCgformFieldMapper)this.baseMapper).executeInsertSQL(params);
    }

    public void editFormData(String code, String tbname, JSONObject json) {
        LambdaQueryWrapper<OnlCgformField> query = new LambdaQueryWrapper();
        query.eq(OnlCgformField::getCgformHeadId, code);
        List<OnlCgformField> fieldList = this.list(query);
        Map<String, Object> params = CgformUtil.getFormDataEditSql(tbname, fieldList, json);
        ((OnlCgformFieldMapper)this.baseMapper).executeUpdatetSQL(params);
    }

    public Map<String, Object> queryFormData(String code, String tbname, String id) {
        LambdaQueryWrapper<OnlCgformField> query = new LambdaQueryWrapper();
        query.eq(OnlCgformField::getCgformHeadId, code);
        query.eq(OnlCgformField::getIsShowForm, 1);
        List<OnlCgformField> fieldList = this.list(query);
        String sql = CgformUtil.getSelectFormSql(tbname, fieldList, id);
        return this.onlCgformFieldMapper.queryFormData(sql);
    }

    public void deleteAutoList(String tbname, String id) {
        this.deleteAutoList(tbname, "id", id);
    }

    public void deleteAutoList(String tbname, String linkField, String linkValue) {
        String[] arr = linkValue.split(",");
        StringBuffer sb = new StringBuffer();
        String[] var6 = arr;
        int var7 = arr.length;

        for(int var8 = 0; var8 < var7; ++var8) {
            String str = var6[var8];
            if (str != null && !"".equals(str)) {
                sb.append("'" + str + "',");
            }
        }

        String temp = sb.toString();
        String sql = "DELETE FROM " + tbname + " where " + linkField + " in(" + temp.substring(0, temp.length() - 1) + ")";
        log.info("--删除sql-->" + sql);
        this.onlCgformFieldMapper.deleteAutoList(sql);
    }

    public List<Map<String, String>> getAutoListQueryInfo(String code) {
        LambdaQueryWrapper<OnlCgformField> query = new LambdaQueryWrapper();
        query.eq(OnlCgformField::getCgformHeadId, code);
        query.eq(OnlCgformField::getIsQuery, 1);
        List<OnlCgformField> fieldList = this.list(query);
        List<Map<String, String>> list = new ArrayList<>();
        int i = 0;

        HashMap map;
        for(Iterator var6 = fieldList.iterator(); var6.hasNext(); list.add(map)) {
            OnlCgformField item = (OnlCgformField)var6.next();
            map = new HashMap();
            map.put("label", item.getDbFieldTxt());
            map.put("view", item.getFieldShowType());
            map.put("mode", item.getQueryMode());
            map.put("field", item.getDbFieldName());
            ++i;
            if (i > 2) {
                map.put("hidden", "1");
            }
        }

        return list;
    }

    public List<OnlCgformField> queryFormFields(String code, boolean isform) {
        LambdaQueryWrapper<OnlCgformField> query = new LambdaQueryWrapper();
        query.eq(OnlCgformField::getCgformHeadId, code);
        if (isform) {
            query.eq(OnlCgformField::getIsShowForm, 1);
        }

        return this.list(query);
    }

    public Map<String, Object> queryFormData(List<OnlCgformField> fieldList, String tbname, String id) {
        String sql = CgformUtil.getSelectFormSql(tbname, fieldList, id);
        return this.onlCgformFieldMapper.queryFormData(sql);
    }

    public List<Map<String, Object>> querySubFormData(List<OnlCgformField> fieldList, String tbname, String linkField, String value) {
        String sql = CgformUtil.getSelectSubFormSql(tbname, fieldList, linkField, value);
        return this.onlCgformFieldMapper.queryListData(sql);
    }

    public IPage<Map<String, Object>> selectPageBySql(Page<Map<String, Object>> page, String sql) {
        return ((OnlCgformFieldMapper)this.baseMapper).selectPageBySql(page, sql);
    }

    public List<String> selectOnlineHideColumns(String tbname) {
        String onlineTbname = "online:" + tbname + "%";
        LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        String userId = sysUser.getId();
        List<String> list = ((OnlCgformFieldMapper)this.baseMapper).selectOnlineHideColumns(userId, onlineTbname);
        List<String> result = new ArrayList<>();
        if (list != null && list.size() != 0 && list.get(0) != null) {
            Iterator var7 = list.iterator();

            while(var7.hasNext()) {
                String combineStr = (String)var7.next();
                if (!oConvertUtils.isEmpty(combineStr)) {
                    String field = combineStr.substring(combineStr.lastIndexOf(":") + 1);
                    if (!oConvertUtils.isEmpty(field)) {
                        result.add(field);
                    }
                }
            }

            return result;
        } else {
            return result;
        }
    }

    public List<OnlCgformField> queryAvailableFields(String cgFormId, String tbname, boolean isList) {
        LambdaQueryWrapper<OnlCgformField> query = new LambdaQueryWrapper();
        query.eq(OnlCgformField::getCgformHeadId, cgFormId);
        if (isList) {
            query.eq(OnlCgformField::getIsShowList, 1);
        } else {
            query.eq(OnlCgformField::getIsShowForm, 1);
        }

        query.orderByAsc(OnlCgformField::getOrderNum);
        List<OnlCgformField> fieldList = this.list(query);
        String onlineTbname = "online:" + tbname + "%";
        LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        String userId = sysUser.getId();
        List<String> hideColumns = ((OnlCgformFieldMapper)this.baseMapper).selectOnlineHideColumns(userId, onlineTbname);
        if (hideColumns != null && hideColumns.size() != 0 && hideColumns.get(0) != null) {
            List<OnlCgformField> result = new ArrayList<>();

            for(int i = 0; i < fieldList.size(); ++i) {
                boolean show = true;
                OnlCgformField field = (OnlCgformField)fieldList.get(i);
                String name = field.getDbFieldName();

                for(int j = 0; j < hideColumns.size(); ++j) {
                    String combineStr = (String)hideColumns.get(j);
                    if (!oConvertUtils.isEmpty(combineStr)) {
                        String hideField = combineStr.substring(combineStr.lastIndexOf(":") + 1);
                        if (!oConvertUtils.isEmpty(hideField) && hideField.equals(name)) {
                            show = false;
                        }
                    }
                }

                if (show) {
                    result.add(field);
                }
            }

            return result;
        } else {
            return fieldList;
        }
    }

    public List<String> queryDisabledFields(String tbname) {
        String onlineTbname = "online:" + tbname + "%";
        LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        String userId = sysUser.getId();
        List<String> result = new ArrayList<>();
        List<String> disabledColumns = ((OnlCgformFieldMapper)this.baseMapper).selectOnlineDisabledColumns(userId, onlineTbname);
        if (disabledColumns != null && disabledColumns.size() != 0 && disabledColumns.get(0) != null) {
            Iterator var7 = disabledColumns.iterator();

            while(var7.hasNext()) {
                String combineStr = (String)var7.next();
                if (!oConvertUtils.isEmpty(combineStr)) {
                    String disabledField = combineStr.substring(combineStr.lastIndexOf(":") + 1);
                    if (!oConvertUtils.isEmpty(disabledField)) {
                        result.add(disabledField);
                    }
                }
            }

            return result;
        } else {
            return result;
        }
    }

    public List<OnlCgformField> queryAvailableFields(String tbname, boolean isList, List<OnlCgformField> List) {
        List<OnlCgformField> result = new ArrayList<>();
        String onlineTbname = "online:" + tbname + "%";
        LoginUser sysUser = (LoginUser)SecurityUtils.getSubject().getPrincipal();
        String userId = sysUser.getId();
        List<String> hideColumns = ((OnlCgformFieldMapper)this.baseMapper).selectOnlineHideColumns(userId, onlineTbname);
        boolean hasConfig = true;
        if (hideColumns == null || hideColumns.size() == 0 || hideColumns.get(0) == null) {
            hasConfig = false;
        }

        Iterator var10 = List.iterator();

        while(true) {
            while(true) {
                OnlCgformField item;
                while(true) {
                    if (!var10.hasNext()) {
                        return result;
                    }

                    item = (OnlCgformField)var10.next();
                    if (isList) {
                        if (item.getIsShowList() != 1) {
                            continue;
                        }
                    } else if (item.getIsShowForm() != 1) {
                        continue;
                    }
                    break;
                }

                if (hasConfig) {
                    boolean show = true;
                    String name = item.getDbFieldName();

                    for(int j = 0; j < hideColumns.size(); ++j) {
                        String combineStr = (String)hideColumns.get(j);
                        if (!oConvertUtils.isEmpty(combineStr)) {
                            String hideField = combineStr.substring(combineStr.lastIndexOf(":") + 1);
                            if (!oConvertUtils.isEmpty(hideField)) {
                                if (hideField.equals(name)) {
                                    show = false;
                                }

                                if (show) {
                                    result.add(item);
                                }
                            }
                        }
                    }
                } else {
                    result.add(item);
                }
            }
        }
    }

    public boolean hasField(String fieldName, List<OnlCgformField> List) {
        boolean flag = false;
        Iterator var4 = List.iterator();

        while(var4.hasNext()) {
            OnlCgformField field = (OnlCgformField)var4.next();
            if (oConvertUtils.camelToUnderline(fieldName).equals(field.getDbFieldName())) {
                flag = true;
                break;
            }
        }

        return flag;
    }
}
